using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using System.Xml;
using PocketLibrary.Blog;
using PocketLibrary;

namespace PocketApps
{
    public partial class LandingForm : Form
    {
        public LandingForm()
        {
            InitializeComponent();
        }

        PocketSettings.PocketSettings milkSettings = null;
        List<IBlog> blogs = new List<IBlog>();

        public void ReadBlogSettingsFrom(string filename)
        {
            try
            {
                blogs.Clear();
                XmlDocument blogsxml = new XmlDocument();

                blogsxml.Load(filename);
                XmlNodeList xmlblogit = blogsxml.GetElementsByTagName("blog");

                for (int i = 0; i < xmlblogit.Count; i++)
                {

                    if (xmlblogit[i].Attributes["type"].Value.ToString() == "nucleus")
                    {

                        Nucleus bblogi = new Nucleus();

                        bblogi.BlogId = int.Parse(xmlblogit[i].Attributes["id"].Value);
                        bblogi.BlogName = xmlblogit[i].Attributes["name"].Value;
                        bblogi.BlogUrl = xmlblogit[i].Attributes["url"].Value;
                        bblogi.ApiUrl = xmlblogit[i].Attributes["apiurl"].Value;

                        XmlNode auth = xmlblogit[i].SelectSingleNode("descendant::authentication");

                        bblogi.UserName = auth.Attributes["username"].Value;
                        bblogi.PassWord = auth.Attributes["password"].Value;

                        XmlNodeList categories = xmlblogit[i].SelectNodes("descendant::category");

                        bblogi.Categories = new Category[categories.Count];

                        for (int ii = 0; ii < categories.Count; ii++)
                        {
                            Category cat = new Category();
                            cat.CategoryId = categories[ii].Attributes["id"].Value;
                            cat.CategoryName = categories[ii].Attributes["name"].Value;

                            bblogi.Categories[ii] = cat;
                        }
                        blogs.Add(bblogi);
                    }
                    else if (xmlblogit[i].Attributes["type"].Value.ToString() == "blogger")
                    {

                        Atom bblogi = new Atom();

                        bblogi.BlogId = 0;
                        bblogi.BlogName = xmlblogit[i].Attributes["name"].Value;
                        bblogi.BlogUrl = xmlblogit[i].Attributes["url"].Value;
                        bblogi.ApiUrl = xmlblogit[i].Attributes["apiurl"].Value;

                        XmlNode auth = xmlblogit[i].SelectSingleNode("descendant::authentication");

                        bblogi.UserName = auth.Attributes["username"].Value;
                        bblogi.PassWord = auth.Attributes["password"].Value;

                        XmlNode servicepost = xmlblogit[i].SelectSingleNode("descendant::servicepost");
                        bblogi.ServicePost = servicepost.Attributes["value"].Value;

                        XmlNode servicefeed = xmlblogit[i].SelectSingleNode("descendant::servicefeed");
                        bblogi.ServiceFeed = servicefeed.Attributes["value"].Value;

                        blogs.Add(bblogi);
                    }
                    else if (xmlblogit[i].Attributes["type"].Value.ToString() == "dokuwiki")
                    {
                        DokuWiki bblogi = new DokuWiki();

                        bblogi.BlogId = 0;
                        bblogi.BlogName = xmlblogit[i].Attributes["name"].Value;
                        bblogi.BlogUrl = xmlblogit[i].Attributes["url"].Value;
                        bblogi.ApiUrl = xmlblogit[i].Attributes["apiurl"].Value;

                        XmlNode auth = xmlblogit[i].SelectSingleNode("descendant::authentication");

                        bblogi.UserName = auth.Attributes["username"].Value;
                        bblogi.PassWord = auth.Attributes["password"].Value;
                        blogs.Add(bblogi);
                    }
                }
            }
            catch { }

        }

        private void createSettings()
        {
            if (milkSettings == null)
            {
                this.milkSettings = PocketSettings.PocketSettings.Create(
                    HelperUtils.ApplicationPath + "settings\\settings.xml"
                    );
            }

        }

        private void LandingForm_Load(object sender, EventArgs e)
        {
            //resolutions = Wimo.Common.Device.Camera.GetAvailableResolutions();
            //camera = new Camera();
            //camera.Open(resolutions[0]);
            //Thread.Sleep(500);
            //pbSelf.Image = camera.TakePicture();
            //camera.Close();
            createSettings();

            buildBlogList(milkSettings);

        }

        private void buildBlogList(PocketSettings.PocketSettings milkSettings)
        {
            createSettings();
            if (milkSettings.Common.Settings.Get("blogsxml") == string.Empty)
            {
                milkSettings.Common.Settings.Add("blogsxml", HelperUtils.ApplicationPath + "settings\\blogs.xml");
                //milkSettings.ToDisk(HelperUtils.ApplicationPath + "settings\\PocketSettings.xml");
                milkSettings.ToDisk();
            }

            ReadBlogSettingsFrom(milkSettings.Common.Settings.Get("blogsxml"));

            cbBlogs.Items.Clear();
            foreach (IBlog iblog in blogs)
            {
                cbBlogs.Items.Add(iblog);
            }

            if (blogs.Count > 1)
                cbBlogs.Items.Add("---------------");

            cbBlogs.Items.Add("Add new blog");
        }

        private void linkBlog_Click(object sender, EventArgs e)
        {
            if (cbBlogs.Items.Count > 1)
            {
            }
            else
            {

                using (BlogManager blogManager = new BlogManager())
                {
                    blogManager.Owner = this;
                    if (blogManager.ShowDialog() == DialogResult.OK)
                    {
                        buildBlogList(milkSettings);
                    }
                }
            }
        }

        private void menuItem3_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void cbBlogs_SelectedIndexChanged(object sender, EventArgs e)
        {
            // selected last item
            if (cbBlogs.SelectedIndex == cbBlogs.Items.Count - 1)
            {
                using (BlogManager blogManager = new BlogManager())
                {
                    blogManager.Owner = this;
                    if (blogManager.ShowDialog() == DialogResult.OK)
                    {
                        buildBlogList(milkSettings);
                    }
                }
            }
            else
            {
                using (BlogWriter blogWriter = new BlogWriter())
                {
                    blogWriter.Owner = this;
                    blogWriter.selectedBlog = (IBlog)cbBlogs.SelectedItem;
                    blogWriter.ShowDialog();
                }
            }
        }

        private void miSettings_Click(object sender, EventArgs e)
        {
            using (WebAppSettings webSettings = new WebAppSettings())
            {
                webSettings.Owner = this;
                this.Text = "Web Application settings";
                webSettings.ShowDialog();
                this.Text = "Pocket Apps";

            }
        }

        private void cbStorage_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cbStorage.SelectedIndex == 0)
            {
                using (BoxNetRestForm boxNet = new BoxNetRestForm())
                {
                    boxNet.Owner = this;
                    this.Text = "Pocket Apps:Box.net";
                    boxNet.ShowDialog();
                    this.Text = "Pocket Apps";
                }
            }
            else if (cbStorage.SelectedIndex == 1)
            {
                using (OpenomyMain openomy = new OpenomyMain())
                {
                    openomy.Owner = this;
                    this.Text = "Pocket Apps:Openomy";
                    openomy.ShowDialog();
                    this.Text = "Pocket Apps";
                }

            }
            else if (cbStorage.SelectedIndex == 2)
            {
                this.Text = "Pocket Apps";

            }
            else if (cbStorage.SelectedIndex == 3)
            {
                this.Text = "Pocket Apps";

            }

        }
    }
}